package com.example.core.controller;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.example.core.po.SysLicenseParam;
import com.example.core.service.SysLicenseParamService;
import com.example.core.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

@RestController
public class SysLicenseParamController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysLicenseParamService sysLicenseParamService;

    /*****
     * 生成许可申请
     *
     * @param subject 授权对象
     * @param expireTime 过期日期 20241108
     * @param notAfter 不超过时间  20241109
     * @param notBefore 不早于时间 20221108
     * @param info 备注信息
     * @return
     */
    @PostMapping(value = "applyLicense")
    public Result applyLicense(@RequestParam String subject, @RequestParam String expireTime, @RequestParam String notAfter, @RequestParam String notBefore, @RequestParam String info) {
        try {
            SysLicenseParam sysLicenseParam = new SysLicenseParam();
            sysLicenseParam.setSubject(subject);
            sysLicenseParam.setExpireTime(DateUtil.parse(expireTime, DatePattern.PURE_DATE_PATTERN));
            sysLicenseParam.setNotAfter(DateUtil.parse(notAfter, DatePattern.PURE_DATE_PATTERN));
            sysLicenseParam.setNotBefore(DateUtil.parse(notBefore, DatePattern.PURE_DATE_PATTERN));
            sysLicenseParam.setInfo(info);
            this.sysLicenseParamService.save(sysLicenseParam);
            return Result.success(sysLicenseParam);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @GetMapping(value = "downloadLicense")
    public void download(@RequestParam String id, HttpServletResponse response) {
        OutputStream outputStream = null;
        try {
            if (id != null) {
                SysLicenseParam sysLicenseParam = this.sysLicenseParamService.getById(id);
                byte[] bytes = this.sysLicenseParamService.download(id);
                response.setCharacterEncoding("UTF-8");
                response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(sysLicenseParam.getLicPath(), "UTF-8"));
                response.addHeader("Content-Length", "" + bytes.length);
                outputStream = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("application/octet-stream");
                outputStream.write(bytes);
                outputStream.flush();
            }
        } catch (Exception e) {
            logger.error("文件下载报错: {}", e.getMessage());
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

